com_c11
membership(com_c11)
table(membership(com_c11))
com_c11.data<-data.frame(table(membership(com_c11)))
com_c11.data
write.csv(com_c11.data,file="com_c11.csv")
#cohort12
net_songofficial_cohort12_ig <- asIgraph(net_songofficial_cohort12)
deg_song_ic12 <- igraph::degree(net_songofficial_cohort12_ig, mode = "all")
deg_song_ic12
com_c12 <- cluster_edge_betweenness(net_songofficial_cohort12_ig)
com_c12
membership(com_c12)
table(membership(com_c12))
com_c12.data<-data.frame(table(membership(com_c12)))
com_c12.data
write.csv(com_c12.data,file="com_c12.csv")
#cohort13
net_songofficial_cohort13_ig <- asIgraph(net_songofficial_cohort13)
deg_song_ic13 <- igraph::degree(net_songofficial_cohort13_ig, mode = "all")
deg_song_ic13
com_c13 <- cluster_edge_betweenness(net_songofficial_cohort13_ig)
com_c13
membership(com_c13)
table(membership(com_c13))
com_c13.data<-data.frame(table(membership(com_c13)))
com_c13.data
write.csv(com_c13.data,file="com_c13.csv")
#cohort14
net_songofficial_cohort14_ig <- asIgraph(net_songofficial_cohort14)
deg_song_ic14 <- igraph::degree(net_songofficial_cohort14_ig, mode = "all")
deg_song_ic14
com_c14 <- cluster_edge_betweenness(net_songofficial_cohort14_ig)
com_c14
membership(com_c14)
table(membership(com_c14))
com_c14.data<-data.frame(table(membership(com_c14)))
com_c14.data
write.csv(com_c14.data,file="com_c14.csv")
#cohort15
net_songofficial_cohort15_ig <- asIgraph(net_songofficial_cohort15)
deg_song_ic15 <- igraph::degree(net_songofficial_cohort15_ig, mode = "all")
deg_song_ic15
com_c15 <- cluster_edge_betweenness(net_songofficial_cohort15_ig)
com_c15
membership(com_c15)
table(membership(com_c15))
com_c15.data<-data.frame(table(membership(com_c15)))
com_c15.data
write.csv(com_c15.data,file="com_c15.csv")
#cohort16
net_songofficial_cohort16_ig <- asIgraph(net_songofficial_cohort16)
deg_song_ic16 <- igraph::degree(net_songofficial_cohort16_ig, mode = "all")
deg_song_ic16
com_c16 <- cluster_edge_betweenness(net_songofficial_cohort16_ig)
com_c16
membership(com_c16)
table(membership(com_c16))
com_c16.data<-data.frame(table(membership(com_c16)))
com_c16.data
write.csv(com_c16.data,file="com_c16.csv")
rm(list = ls())
library(foreign)
library(tidyverse)
library(ggrepel)
library(plyr)
require(scales)
require(gridExtra)
#Change this to your local directory#
setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Appendices/data/")
frac <- read.dta("fractionalization by cohort.dta")
attach(frac)
ggplot(data=frac, aes(x=start_year, y=factionalization, color="grey",linetype="solid")) +
geom_point() + geom_line() +
scale_x_continuous(name="Year of Becoming Major Official", limits=c(960,1260), breaks=c(960,1000,1040,1080,1120,1160,1200,1240)) +
scale_y_continuous(name="Social Fractionalization of Officials' Marriage Network",limits=c(0,1), breaks=c(0,0.5,1)) +
geom_vline(xintercept = 1127,color="black",linetype="dashed")+
annotate(geom="text", x = 1040, y=0.6, label = "Northern Song",size=4)+
annotate(geom="text", x = 1200, y=0.6, label = "Southern Song", size=4)+
theme_bw()+
theme(legend.position="none")+
theme(
axis.title.y = element_text(size = 8)
)
rm(list = ls())
library(statnet)
library("network")
set.seed(12345)
#Change this to your local directory#
setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Appendices/data/")
#Read in Wang Anshi Reform's Major Officials' Adjacency Matrix
net.adj.child3<-read.csv(file="major officials adj matrix.csv",header=FALSE,stringsAsFactors=FALSE)
net.adj.child3 <- as.matrix(net.adj.child3)
# load in vertex attributes
nodeInfoCHILD3 <- read.csv(file="vertexAttributes.csv",header=TRUE,stringsAsFactors=FALSE)
print(net.adj.child3) # peek at matrix
print(nodeInfoCHILD3)  # peek at attribute data
# Since our relational data has no row/column names, let's set them now
rownames(net.adj.child3) <- nodeInfoCHILD3$name_pinyin
colnames(net.adj.child3) <- nodeInfoCHILD3$name_pinyin
print(net.adj.child3)
# create undirected network object from matrix
net_reformofficial_child3<-network(net.adj.child3, directed=FALSE, matrix.type="adjacency")
# it read in vertex names from matrix col names ...
network.vertex.names(net_reformofficial_child3)
# ATTACHING VERTEX ATTRIBUTES
# ... but could also set vertex.names with
net_reformofficial_child3%v%'vertex.names'<- nodeInfoCHILD3$name_pinyin
# load in other attributes
net_reformofficial_child3%v%"support" <- nodeInfoCHILD3$policy_opinion_tri
# Note: order of attributes in the data frame MUST match vertex ids
# otherwise the attribute will get assigned to the wrong vertex
# check that they got loaded
list.vertex.attributes(net_reformofficial_child3)
summary(net_reformofficial_child3)
gden(net_reformofficial_child3)
# make my color palette
library(RColorBrewer)
addalpha <- function(colors, alpha=1.0) {
r <- col2rgb(colors, alpha=T)
# Apply alpha
r[4,] <- alpha*255
r <- r/255.0
return(rgb(r[1,], r[2,], r[3,], r[4,]))
}
display.brewer.all()
display.brewer.pal(3, "RdYlGn")
my_pal<-brewer.pal(3,"RdYlGn")
# assign different colors to supports and opponents
rolecat_child3<-as.factor(get.vertex.attribute(net_reformofficial_child3,"support"))
# centrality measures
#Degree centrality#
deg3_child<-degree(net_reformofficial_child3,gmode="graph")
deg3_child
deg3_child.data<-data.frame(deg3_child)
deg3_child.data
#Betweenness centrality#
btw3_child<-betweenness(net_reformofficial_child3,gmode="graph")
btw3_child
btw3_child.data<-data.frame(btw3_child)
btw3_child.data
#Bonacich centrality#
bonacich3_child<-bonpow(net_reformofficial_child3,gmode="graph")
bonacich3_child
bonacich3_child.data<-data.frame(bonacich3_child)
bonacich3_child.data
# Network graph
rolelab_child3<-get.vertex.attribute(net_reformofficial_child3,"support")
library(igraph)
library(gcookbook)
rescale <- function(nchar,low,high) {
min_d <- min(nchar)
max_d <- max(nchar)
rscl <- ((high-low)*(nchar-min_d))/(max_d-min_d)+low
rscl}
op<-par(mar=c(0,0,0,0))
gplot(net_reformofficial_child3, gmode="graph", mode='fruchtermanreingold', displaylabels=F,
pad=0.4, vertex.cex=rescale(btw3_child,1,4), edge.col="grey90",
vertex.col=addalpha(my_pal[rolecat_child3], 0.4))
legend("topleft",legend=c("Oppose","Unkown","Support"),
col=my_pal,pch=19,pt.cex=1.5,bty="n")
par(op)
rm(list = ls())
library(statnet)
library("network")
set.seed(12345)
#Change this to your local directory#
setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Appendices/data/")
#Read in Wang Anshi Reform's Major Officials' Adjacency Matrix
net.adj.child3<-read.csv(file="major officials adj matrix.csv",header=FALSE,stringsAsFactors=FALSE)
net.adj.child3 <- as.matrix(net.adj.child3)
# load in vertex attributes
nodeInfoCHILD3 <- read.csv(file="vertexAttributes.csv",header=TRUE,stringsAsFactors=FALSE)
print(net.adj.child3) # peek at matrix
print(nodeInfoCHILD3)  # peek at attribute data
# Since our relational data has no row/column names, let's set them now
rownames(net.adj.child3) <- nodeInfoCHILD3$name_pinyin
colnames(net.adj.child3) <- nodeInfoCHILD3$name_pinyin
print(net.adj.child3)
net_reformofficial_child3<-network(net.adj.child3, directed=FALSE, matrix.type="adjacency")
# it read in vertex names from matrix col names ...
network.vertex.names(net_reformofficial_child3)
# ATTACHING VERTEX ATTRIBUTES
# ... but could also set vertex.names with
net_reformofficial_child3%v%'vertex.names'<- nodeInfoCHILD3$name_pinyin
# load in other attributes
net_reformofficial_child3%v%"support" <- nodeInfoCHILD3$policy_opinion_tri
# check that they got loaded
list.vertex.attributes(net_reformofficial_child3)
summary(net_reformofficial_child3)
gden(net_reformofficial_child3)
library(RColorBrewer)
addalpha <- function(colors, alpha=1.0) {
r <- col2rgb(colors, alpha=T)
# Apply alpha
r[4,] <- alpha*255
r <- r/255.0
return(rgb(r[1,], r[2,], r[3,], r[4,]))
}
display.brewer.all()
display.brewer.pal(3, "RdYlGn")
my_pal<-brewer.pal(3,"RdYlGn")
rolecat_child3<-as.factor(get.vertex.attribute(net_reformofficial_child3,"support"))
rm(list = ls())
library(statnet)
library("network")
library(igraph)
set.seed(12345)
#Change this to your local directory#
setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Appendices/data/")
#Read in Wang Anshi Reform's Major Officials' Adjacency Matrix
net.adj.child3<-read.csv(file="major officials adj matrix.csv",header=FALSE,stringsAsFactors=FALSE)
net.adj.child3 <- as.matrix(net.adj.child3)
# load in vertex attributes
nodeInfoCHILD3 <- read.csv(file="vertexAttributes.csv",header=TRUE,stringsAsFactors=FALSE)
print(net.adj.child3) # peek at matrix
print(nodeInfoCHILD3)  # peek at attribute data
# Since our relational data has no row/column names, let's set them now
rownames(net.adj.child3) <- nodeInfoCHILD3$name_pinyin
colnames(net.adj.child3) <- nodeInfoCHILD3$name_pinyin
print(net.adj.child3)
# create undirected network object from matrix
net_reformofficial_child3<-network(net.adj.child3, directed=FALSE, matrix.type="adjacency")
# it read in vertex names from matrix col names ...
network.vertex.names(net_reformofficial_child3)
# ATTACHING VERTEX ATTRIBUTES
# ... but could also set vertex.names with
net_reformofficial_child3%v%'vertex.names'<- nodeInfoCHILD3$name_pinyin
# load in other attributes
net_reformofficial_child3%v%"support" <- nodeInfoCHILD3$policy_opinion_tri
# Note: order of attributes in the data frame MUST match vertex ids
# otherwise the attribute will get assigned to the wrong vertex
# check that they got loaded
list.vertex.attributes(net_reformofficial_child3)
summary(net_reformofficial_child3)
gden(net_reformofficial_child3)
# make my color palette
library(RColorBrewer)
addalpha <- function(colors, alpha=1.0) {
r <- col2rgb(colors, alpha=T)
# Apply alpha
r[4,] <- alpha*255
r <- r/255.0
return(rgb(r[1,], r[2,], r[3,], r[4,]))
}
display.brewer.all()
display.brewer.pal(3, "RdYlGn")
my_pal<-brewer.pal(3,"RdYlGn")
rolecat_child3<-as.factor(get.vertex.attribute(net_reformofficial_child3,"support"))
#Degree centrality#
deg3_child<-degree(net_reformofficial_child3,gmode="graph")
deg3_child
rm(list = ls())
library(statnet)
library("network")
setwd("~/Dropbox/Book Project on State Building/Wang Anshi reform article/Data/working/network analysis/3/")
set.seed(12345)
net.adj.child3<-read.csv(file="major officials adj matrix via children3.csv",header=FALSE,stringsAsFactors=FALSE)
net.adj.child3 <- as.matrix(net.adj.child3)
# load in vertex attributes
nodeInfoCHILD3 <- read.csv(file="vertexAttributesCHILD3.csv",header=TRUE,stringsAsFactors=FALSE)
print(net.adj.child3) # peek at matrix
print(nodeInfoCHILD3)  # peek at attribute data
# Since our relational data has no row/column names, let's set them now
rownames(net.adj.child3) <- nodeInfoCHILD3$name_pinyin
colnames(net.adj.child3) <- nodeInfoCHILD3$name_pinyin
print(net.adj.child3)
# create undirected network object from matrix
net_reformofficial_child3<-network(net.adj.child3, directed=FALSE, matrix.type="adjacency")
# it read in vertex names from matrix col names ...
network.vertex.names(net_reformofficial_child3)
# ATTACHING VERTEX ATTRIBUTES
# ... but could also set vertex.names with
net_reformofficial_child3%v%'vertex.names'<- nodeInfoCHILD3$name_pinyin
# load in other attributes
net_reformofficial_child3%v%"support" <- nodeInfoCHILD3$policy_opinion_tri
net_reformofficial_child3%v%"policy" <- nodeInfoCHILD3$policy_att
net_reformofficial_child3%v%"fiscal" <- nodeInfoCHILD3$off_fin
# Note: order of attributes in the data frame MUST match vertex ids
# otherwise the attribute will get assigned to the wrong vertex
# check that they got loaded
list.vertex.attributes(net_reformofficial_child3)
summary(net_reformofficial_child3)
gden(net_reformofficial_child3)
library(RColorBrewer)
display.brewer.all()
display.brewer.pal(3, "RdYlGn")
my_pal<-brewer.pal(3,"RdYlGn")
rolecat_child3<-as.factor(get.vertex.attribute(net_reformofficial_child3,"support"))
rm(list = ls())
library(statnet)
library("network")
library(igraph)
set.seed(12345)
#Change this to your local directory#
setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Appendices/data/")
#Read in Wang Anshi Reform's Major Officials' Adjacency Matrix
net.adj.child3<-read.csv(file="major officials adj matrix.csv",header=FALSE,stringsAsFactors=FALSE)
net.adj.child3 <- as.matrix(net.adj.child3)
# load in vertex attributes
nodeInfoCHILD3 <- read.csv(file="vertexAttributes.csv",header=TRUE,stringsAsFactors=FALSE)
print(net.adj.child3) # peek at matrix
print(nodeInfoCHILD3)  # peek at attribute data
# Since our relational data has no row/column names, let's set them now
rownames(net.adj.child3) <- nodeInfoCHILD3$name_pinyin
colnames(net.adj.child3) <- nodeInfoCHILD3$name_pinyin
print(net.adj.child3)
# create undirected network object from matrix
net_reformofficial_child3<-network(net.adj.child3, directed=FALSE, matrix.type="adjacency")
# it read in vertex names from matrix col names ...
network.vertex.names(net_reformofficial_child3)
# ATTACHING VERTEX ATTRIBUTES
# ... but could also set vertex.names with
net_reformofficial_child3%v%'vertex.names'<- nodeInfoCHILD3$name_pinyin
# load in other attributes
net_reformofficial_child3%v%"support" <- nodeInfoCHILD3$policy_opinion_tri
# Note: order of attributes in the data frame MUST match vertex ids
# otherwise the attribute will get assigned to the wrong vertex
# check that they got loaded
list.vertex.attributes(net_reformofficial_child3)
summary(net_reformofficial_child3)
gden(net_reformofficial_child3)
# make my color palette
library(RColorBrewer)
addalpha <- function(colors, alpha=1.0) {
r <- col2rgb(colors, alpha=T)
# Apply alpha
r[4,] <- alpha*255
r <- r/255.0
return(rgb(r[1,], r[2,], r[3,], r[4,]))
}
display.brewer.all()
display.brewer.pal(3, "RdYlGn")
my_pal<-brewer.pal(3,"RdYlGn")
detach("package:igraph", unload=TRUE)
# assign different colors to supports and opponents
rolecat_child3<-as.factor(get.vertex.attribute(net_reformofficial_child3,"support"))
rm(list = ls())
library(statnet)
library("network")
set.seed(12345)
#Read in Wang Anshi Reform's Major Officials' Adjacency Matrix
net.adj.child3<-read.csv(file="major officials adj matrix.csv",header=FALSE,stringsAsFactors=FALSE)
net.adj.child3 <- as.matrix(net.adj.child3)
# load in vertex attributes
nodeInfoCHILD3 <- read.csv(file="vertexAttributes.csv",header=TRUE,stringsAsFactors=FALSE)
print(net.adj.child3) # peek at matrix
print(nodeInfoCHILD3)  # peek at attribute data
# Since our relational data has no row/column names, let's set them now
rownames(net.adj.child3) <- nodeInfoCHILD3$name_pinyin
colnames(net.adj.child3) <- nodeInfoCHILD3$name_pinyin
print(net.adj.child3)
# create undirected network object from matrix
net_reformofficial_child3<-network(net.adj.child3, directed=FALSE, matrix.type="adjacency")
# it read in vertex names from matrix col names ...
network.vertex.names(net_reformofficial_child3)
# ATTACHING VERTEX ATTRIBUTES
# ... but could also set vertex.names with
net_reformofficial_child3%v%'vertex.names'<- nodeInfoCHILD3$name_pinyin
# load in other attributes
net_reformofficial_child3%v%"support" <- nodeInfoCHILD3$policy_opinion_tri
# Note: order of attributes in the data frame MUST match vertex ids
# otherwise the attribute will get assigned to the wrong vertex
# check that they got loaded
list.vertex.attributes(net_reformofficial_child3)
summary(net_reformofficial_child3)
gden(net_reformofficial_child3)
# make my color palette
library(RColorBrewer)
addalpha <- function(colors, alpha=1.0) {
r <- col2rgb(colors, alpha=T)
# Apply alpha
r[4,] <- alpha*255
r <- r/255.0
return(rgb(r[1,], r[2,], r[3,], r[4,]))
}
display.brewer.all()
display.brewer.pal(3, "RdYlGn")
my_pal<-brewer.pal(3,"RdYlGn")
# assign different colors to supports and opponents
detach("package:igraph", unload=TRUE)
rolecat_child3<-as.factor(get.vertex.attribute(net_reformofficial_child3,"support"))
#Degree centrality#
deg3_child<-degree(net_reformofficial_child3,gmode="graph")
deg3_child
deg3_child.data<-data.frame(deg3_child)
deg3_child.data
#Betweenness centrality#
btw3_child<-betweenness(net_reformofficial_child3,gmode="graph")
btw3_child
btw3_child.data<-data.frame(btw3_child)
btw3_child.data
#Bonacich centrality#
bonacich3_child<-bonpow(net_reformofficial_child3,gmode="graph")
bonacich3_child
bonacich3_child.data<-data.frame(bonacich3_child)
bonacich3_child.data
# Network graph
rolelab_child3<-get.vertex.attribute(net_reformofficial_child3,"support")
library(igraph)
library(gcookbook)
rescale <- function(nchar,low,high) {
min_d <- min(nchar)
max_d <- max(nchar)
rscl <- ((high-low)*(nchar-min_d))/(max_d-min_d)+low
rscl}
op<-par(mar=c(0,0,0,0))
gplot(net_reformofficial_child3, gmode="graph", mode='fruchtermanreingold', displaylabels=F,
pad=0.4, vertex.cex=rescale(btw3_child,1,4), edge.col="grey90",
vertex.col=addalpha(my_pal[rolecat_child3], 0.4))
legend("topleft",legend=c("Oppose","Unkown","Support"),
col=my_pal,pch=19,pt.cex=1.5,bty="n")
par(op)
rm(list = ls())
library(statnet)
library("network")
library(igraph)
set.seed(12345)
#Change this to your local directory#
setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Appendices/data/")
#Read in Wang Anshi Reform's Major Officials' Adjacency Matrix
net.adj.child3<-read.csv(file="major officials adj matrix.csv",header=FALSE,stringsAsFactors=FALSE)
net.adj.child3 <- as.matrix(net.adj.child3)
# load in vertex attributes
nodeInfoCHILD3 <- read.csv(file="vertexAttributes.csv",header=TRUE,stringsAsFactors=FALSE)
print(net.adj.child3) # peek at matrix
print(nodeInfoCHILD3)  # peek at attribute data
# Since our relational data has no row/column names, let's set them now
rownames(net.adj.child3) <- nodeInfoCHILD3$name_pinyin
colnames(net.adj.child3) <- nodeInfoCHILD3$name_pinyin
print(net.adj.child3)
# create undirected network object from matrix
net_reformofficial_child3<-network(net.adj.child3, directed=FALSE, matrix.type="adjacency")
# it read in vertex names from matrix col names ...
network.vertex.names(net_reformofficial_child3)
# ATTACHING VERTEX ATTRIBUTES
# ... but could also set vertex.names with
net_reformofficial_child3%v%'vertex.names'<- nodeInfoCHILD3$name_pinyin
# load in other attributes
net_reformofficial_child3%v%"support" <- nodeInfoCHILD3$policy_opinion_tri
# Note: order of attributes in the data frame MUST match vertex ids
# otherwise the attribute will get assigned to the wrong vertex
# check that they got loaded
list.vertex.attributes(net_reformofficial_child3)
summary(net_reformofficial_child3)
gden(net_reformofficial_child3)
# make my color palette
library(RColorBrewer)
addalpha <- function(colors, alpha=1.0) {
r <- col2rgb(colors, alpha=T)
# Apply alpha
r[4,] <- alpha*255
r <- r/255.0
return(rgb(r[1,], r[2,], r[3,], r[4,]))
}
display.brewer.all()
display.brewer.pal(3, "RdYlGn")
my_pal<-brewer.pal(3,"RdYlGn")
detach("package:igraph", unload=TRUE)
# assign different colors to supports and opponents
rolecat_child3<-as.factor(get.vertex.attribute(net_reformofficial_child3,"support"))
#Degree centrality#
deg3_child<-degree(net_reformofficial_child3,gmode="graph")
deg3_child
deg3_child.data<-data.frame(deg3_child)
deg3_child.data
#Betweenness centrality#
btw3_child<-betweenness(net_reformofficial_child3,gmode="graph")
btw3_child
btw3_child.data<-data.frame(btw3_child)
btw3_child.data
#Bonacich centrality#
bonacich3_child<-bonpow(net_reformofficial_child3,gmode="graph")
bonacich3_child
bonacich3_child.data<-data.frame(bonacich3_child)
bonacich3_child.data
# Network graph
rolelab_child3<-get.vertex.attribute(net_reformofficial_child3,"support")
library(igraph)
library(gcookbook)
rescale <- function(nchar,low,high) {
min_d <- min(nchar)
max_d <- max(nchar)
rscl <- ((high-low)*(nchar-min_d))/(max_d-min_d)+low
rscl}
op<-par(mar=c(0,0,0,0))
gplot(net_reformofficial_child3, gmode="graph", mode='fruchtermanreingold', displaylabels=F,
pad=0.4, vertex.cex=rescale(btw3_child,1,4), edge.col="grey90",
vertex.col=addalpha(my_pal[rolecat_child3], 0.4))
legend("topleft",legend=c("Oppose","Unkown","Support"),
col=my_pal,pch=19,pt.cex=1.5,bty="n")
par(op)
rm(list = ls())
library(foreign)
library(ggplot2)
require(ggplot2)
library(ggrepel)
library(plyr)
require(scales)
#Change this to your local directory#
setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Appendices/data/")
singlewhip <- read.csv(file="Single Whip Reform data.csv", header=TRUE, sep=",")
attach(singlewhip)
ggplot(singlewhip, aes(x=presentedscholar_pre1573, y=politician)) +
geom_point(shape=19, color="violetred4", size=3)+
geom_smooth(method=lm,  se=TRUE,linetype="solid",
color="deepskyblue4", fill="deepskyblue2")+
scale_y_continuous(name="N of Major Officials, 1573-1620",limits=c(0,20), breaks=c(0,5,10,15,20))+
scale_x_continuous(name="N of Advanced Scholars, 1368-1572", limits=c(0,600), breaks=c(0,50,100,150,200,250,300,350,400,450,500,550,600))+
theme(text = element_text(size=15,
vjust=1, family="Times"))+
theme_bw()
